{% comment %}
Assumes distro and package are defined
{% endcomment %}

{% if package.data.deprecated and package.data.deprecated != '' %}
  <div class="alert alert-warning"><h3><strong>Package Deprecated</strong></h3> {{package.data.deprecated}}</div>
{% endif %}

<ul class="nav nav-tabs nav-justified" id="{{distro}}-tabs" style="margin-bottom:20px">
  <li class="better-tabs active">
    <a href="#{{distro}}-overview" data-toggle="tab">Overview</a>
  </li>
  <li class="better-tabs">
    {% assign n_launch = package.data.launch_data | size %}
    {% assign n_msgs = package.data.msg_files | size %}
    {% assign n_srvs = package.data.srv_files | size %}
    {% assign n_plugins = package.data.plugin_data | size %}
    {% assign n_assets = n_launch | plus: n_srvs | plus: n_msgs | plus: n_plugins %}
    <a href="#{{distro}}-assets" data-toggle="tab"><span class="label label-{%if n_assets == 0%}default{%else%}primary{%endif%}">{{n_assets}}</span> Assets</a>
  </li>
  <li class="better-tabs">
    {% assign n_dependants = package.data.dependants | size %}
    {% assign n_pkg_deps = package.data.pkg_deps | size %}
    {% assign n_system_deps = package.data.system_deps | size %}
    {% assign n_deps = n_pkg_deps | plus: n_system_deps %}
    <a href="#{{distro}}-deps" data-toggle="tab"><span class="label label-{%if n_deps == 0%}default{%else%}primary{%endif%}">{{n_deps}}</span> Dependencies</a>
  </li>
  <li class="better-tabs">
    {% assign n_tutorials = package.data.wiki.tutorials | size %}
    <a href="#{{distro}}-tutorials" data-toggle="tab"><span class="label label-{%if n_tutorials == 0%}default{%else%}primary{%endif%}">{{n_tutorials}}</span> Tutorials</a>
  </li>
  <li class="better-tabs">
    <a href="#{{distro}}-questions" data-toggle="tab"><span id="{{distro}}-questions-count" class="label label-primary">0</span> Q & A</a>
  </li>
</ul>

<div class="tab-content">
  <div class="tab-pane active" id="{{distro}}-overview">
    {% if package.repo.release_manifests[distro] %}
      <div class="row">
        <div class="col-xs-12">
          <div class="alert alert-info">
            <h4>Third-Party Package</h4>
            This third-party package's source repository does not contain a package manifest. Instead, its package manifest is stored in its <em>release repository</em>.
            In order to build this package from source in a Catkin workspace, please <a class="alert-link" target="_blank" href="{{site.baseurl}}/p/{{package.name}}/{{package.repo.id}}/{{distro}}/package.xml">download its package manifest</a>.
          </div>
        </div>
      </div>
    {% endif %}
    <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
        <div class="panel panel-default">
          <div class="panel-heading"><h4 class="panel-title">Package Summary</h4></div>
          <div class="panel-body">
            <table class="table table-condensed">
              <tr>
                <td style="width:100px;" class="text-right"><strong>Tags</strong></td>
                <td>
                  {% assign n_tags = package.data.tags.size %}
                  {% if n_tags > 0 %}
                    {% for tag in package.data.tags %}<span class="label label-default">{{ tag }}</span> {% endfor %}
                  {% else %}
                    <em>No category tags.</em>
                  {% endif %}
                </td>
              </tr>
              <tr>
                <td class="text-right"><strong>Version</strong></td>
                <td>{{ package.data.version }}</td>
              </tr>
              <tr>
                <td class="text-right"><strong>License</strong></td>
                <td>{{ package.data.license }}</td>
              </tr>
              <tr>
                <td class="text-right"><strong>Build type</strong></td>
                <td>
                  {% if package.data.pkg_type == 'catkin' %}
                    <span class="label label-info">
                  {% else %}
                    <span class="label label-default">
                  {% endif %}
                    {{package.data.pkg_type | upcase}}</span>
                </td>
              </tr>
              <tr>
                <td class="text-right"><strong>Use</strong></td>
                <td>
                {% if package.data.deprecated and package.data.deprecated != '' %}
                  <span class="label label-warning">DEPRECATED</span>
                {%else%}
                  <span class="label label-success">RECOMMENDED</span>
                {%endif%}
                </td>
              </tr>
            </table>
          </div>
          <div class="panel-heading"><h3 class="panel-title">Repository Summary</h3></div>
          <div class="panel-body">
            {% assign repo = package.repo %}
            {% assign snapshot = package.snapshot %}
            {% include repo_summary.html %}
          </div>
        </div>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
        <div class="panel panel-default">
          <div class="panel-heading"><h3 class="panel-title">Package Description</h3></div>
          <div class="panel-body">
            {{ package.data.description}}
          </div>
          <div class="panel-heading"><h4 class="panel-title">Additional Links</h4></div>
          <div class="panel-body">
            {% assign n_urls = package.data.urls | size %}
            {% if n_urls > 0 %}
              <ul class="list-unstyled">
                {% for url in package.data.urls %}
                  <li><a href="{{url.uri}}">{%if url.type%}{{url.type | capitalize}}{%else%}{{url.domain}}{%endif%}</a></li>
                {% endfor %}
              </ul>
            {% else %}
              <em>No additional links.</em>
            {% endif %}
          </div>
          <div class="panel-heading"><h4 class="panel-title">Maintainers</h4></div>
          <div class="panel-body">
            {% assign n_maintainers = package.data.maintainers | size %}
            {% if n_maintainers > 0 %}
              <ul class="list-unstyled">
                {% for m in package.data.maintainers %}
                  <li>{{m | replace: '@', ' [AT] ' }}</li>
                {% endfor %}
              </ul>
            {% else %}
              <em>No known maintainers.</em>
            {% endif %}
          </div>
          <div class="panel-heading"><h4 class="panel-title">Authors</h4></div>
          <div class="panel-body">
            {% assign n_authors = package.data.authors | size %}
            {% if n_authors > 0 %}
              <ul class="list-unstyled">
                {% for a in package.data.authors %}
                  <li>{{a | replace: '@', ' [AT] ' }}</li>
                {% endfor %}
              </ul>
            {% else %}
              <em>No additional authors.</em>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
    {% unless package.data.readmes == empty %}
      {% for readme in package.data.readmes %}
        <div class="row">
          <div class="col-xs-12">
            <div class="panel panel-default">
              <div class="panel-heading"><span class="glyphicon glyphicon-file"></span><a href="{{readme.browse_uri}}" title="Open in git repository">{{ readme.relpath }}</a></div>
              <div class="panel-body">
                    {{ readme.readme_rendered }}
              </div>
            </div>
          </div>
        </div>
      {% endfor %}
    {% else %}
      <div class="row">
        <div class="col-xs-12">
          <div class="panel panel-default">
            <div class="panel-heading"><span class="glyphicon glyphicon-file"></span> README </div>
            <div class="panel-body">
            <em>No README found.</em>
            {% if package.snapshot.data.readme_rendered %} <em><a href="{{site.baseurl}}/r/{{package.repo.name}}/{{package.repo.id}}/#{{distro}}">See repository README.</a></em>
            {% else %}
              <em>No README in repository either.</em>
            {% endif %}
            </div>
          </div>
        </div>
      </div>
    {% endunless %}
    <div class="row">
      <div class="col-xs-12">
        <div class="panel panel-default">
          <div class="panel-heading"><span class="glyphicon glyphicon-list"></span> CHANGELOG</div>
          <div class="panel-body">
            {% if package.data.changelog_rendered %}
              {{ package.data.changelog_rendered }}
            {% else %}
              <em>No CHANGELOG found.</em>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="tab-pane" id="{{distro}}-tutorials">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Wiki Tutorials</h3>
      </div>
      <div class="panel-body">
        {% if n_tutorials > 0 %}
          <ul>
            {% for t in package.data.wiki.tutorials %}
              <li><a href="http://wiki.ros.org/{{t[1]}}" target="_blank">{{t[0]}}</a></li>
            {% endfor %}
          </ul>
          <br>
        {% endif %}
        <em>See <a href="http://wiki.ros.org/{{package.name}}/Tutorials" target="_blank">ROS Wiki Tutorials</a> for more details.</em>
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Source Tutorials</h3>
      </div>
      <div class="panel-body">
        <em>Not currently indexed.</em>
      </div>
    </div>
  </div>

  <div class="tab-pane" id="{{distro}}-deps">
    <div class="row">
      <div class="col-sm-5">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">Package Dependencies</h3>
          </div>
          <div class="panel-body">
            {% if n_pkg_deps > 0 %}
              <table class="table table-condensed table-striped">
                <thead>
                  <th class="text-center">Deps</th>
                  <th class="text-center"><span class="glyphicon glyphicon-th" title="Package instances"></span></th>
                  <th style="width: 100%">Name</th>
                </thead>
                <tbody>
                  {% for p in package.data.pkg_deps %}
                    {% assign n_instances = p[1].instances | size %}
                    {% if n_instances > 0 %}
                      <tr>
                        <td class="text-center">
                          {% assign n_2nd_order_pdeps = p[1].snapshots[distro].data.pkg_deps | size %}
                          {% assign n_2nd_order_sdeps = p[1].snapshots[distro].data.system_deps | size %}
                          {% assign n_2nd_order_deps = n_2nd_order_pdeps | plus: n_2nd_order_sdeps %}
                          <a href="{{site.baseurl}}/p/{{p[0]}}#{{distro}}-deps" {% if n_2nd_order_deps == 0 %}class="inactive"{% endif %}>
                            <span class="glyphicon glyphicon-arrow-left" title="Package dependencies"></span>
                          </a>
                        </td>
                        <td class="text-center">
                          <a href="{{site.baseurl}}/packages/{{p[0]}}" class="label label-{% if n_instances > 1 %}primary{% else %}default{% endif %}">
                            {{n_instances}}
                          </a>
                        </td>
                        <td><a href="{{site.baseurl}}/p/{{p[0]}}#{{distro}}">{{p[0]}}</a></td>
                      </tr>
                    {% else %}
                      <tr>
                        <td class="text-center"></td>
                        <td class="text-center">
                          <span class="label label-danger">0</span>
                        </td>
                        <td>{{p[0]}}</td>
                      </tr>
                    {% endif %}
                  {%endfor%}
                </tbody>
              </table>
            {% else %}
              <em>No dependencies on ROS packages.</em>
            {% endif %}
          </div>
          <div class="panel-heading">
            <h3 class="panel-title">System Dependencies</h3>
          </div>
          <div class="panel-body">
            {% if n_system_deps > 0 %}
              <table class="table table-condensed table-striped">
                <thead>
                  <th>Name</th>
                </thead>
                <tbody>
                  {% for d in package.data.system_deps %}
                    {% if d[1] %}
                      <tr>
                        <td><a href="{{site.baseurl}}/d/{{d[0]}}">{{d[0]}}</a></td>
                      </tr>
                    {% else %}
                      <tr class="danger">
                        <td>{{d[0]}}</td>
                      </tr>
                    {% endif %}
                  {%endfor%}
                </tbody>
              </table>
            {% else %}
              <em>No direct system dependencies.</em>
            {% endif %}
          </div>
        </div>
      </div>

      <div class="col-sm-7">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">Dependant Packages</h3>
          </div>
          <div class="panel-body">
            {% if n_dependants > 0 %}
              <table class="table table-condensed table-striped">
                <thead>
                  <th>Name</th>
                  <th>Repo</th>
                  <th class="text-center">Deps</th>
                </thead>
                <tbody>
                  {% for d in package.data.dependants %}
                    {% assign dep_name = d[0] %}
                    {% for dep_instance in d[1] %}
                    <tr>
                      <td><a href="{{site.baseurl}}/p/{{dep_name}}/{{dep_instance.id}}#{{distro}}">{{dep_name}}</a></td>
                      <td><a href="{{site.baseurl}}/r/{{dep_instance.repo.name}}/{{dep_instance.id}}">{{dep_instance.id}}</a></td>
                      <td class="text-center">
                        {% assign n_2nd_order_deps = dep_instance.package.data.dependants | size %}
                        <a href="{{site.baseurl}}/p/{{dep_name}}/{{dep_instance.id}}#{{distro}}-deps" {% if n_2nd_order_deps == 0 %}class="inactive"{% endif %}>
                          <span class="glyphicon glyphicon-arrow-right"></span>
                        </a>
                      </td>
                    </tr>
                    {% endfor %}
                  {%endfor%}
                </tbody>
              </table>
            {% else %}
              <em>No known dependants.</em>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="tab-pane" id="{{distro}}-assets">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Launch files</h3>
      </div>
      <div class="panel-body">
        {% if n_launch > 0 %}
          <ul>
            {% for f in package.data.launch_data %}
              <li><a href="{{package.data.browse_uri}}/{{f.relpath}}" target="_blank">{{f.relpath}}</a>
                <ul class="list-unstyled">
                  {% if f.comment %}
                    <li><em>{{f.comment}}</em></li>
                  {% endif %}
                  <li>
                    <ul>
                      {% for a in f.args %}
                        {% unless a.value %}
                          <li><strong>{{a.name}}</strong> {%if a.default%}<em>&#91;default: {{a.default}}&#93;{%endif%} {% if a.doc%} &#8212; {{a.doc}}{%endif%}</em></li>
                        {% endunless %}
                      {% endfor %}
                    </ul>
                  </li>
                </ul>
              </li>
            {%endfor%}
          </ul>
        {% else %}
          <em>No launch files found</em>
        {% endif %}
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Messages</h3>
      </div>
      <div class="panel-body">
        {% if n_msgs > 0 %}
          <ul>
            {% for f in package.data.msg_files %}
              <li><a href="{{package.data.browse_uri}}/{{f}}" target="_blank">{{f}}</a></li>
            {%endfor%}
          </ul>
        {% else %}
          <em>No message files found.</em>
        {% endif %}
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Services</h3>
      </div>
      <div class="panel-body">
        {% if n_srvs > 0 %}
          <ul>
            {% for f in package.data.srv_files %}
              <li><a href="{{package.data.browse_uri}}/{{f}}" target="_blank">{{f}}</a></li>
            {%endfor%}
          </ul>
        {% else %}
          <em>No service files found</em>
        {% endif %}
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Plugins</h3>
      </div>
      <div class="panel-body">
        {% if n_plugins > 0 %}
          <ul>
            {% for f in package.data.plugin_data %}
              <li><a href="{{package.data.browse_uri}}/{{f.file}}" target="_blank">{{f.name}}</a></li>
            {%endfor%}
          </ul>
        {% else %}
          <em>No plugins found.</em>
        {% endif %}
      </div>
    </div>
  </div>

  <div class="tab-pane" id="{{distro}}-questions">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Recent questions tagged <kbd>{{package.name}}</kbd> at <strong><a href="https://robotics.stackexchange.com/" target="_blank">Robotics Stack Exchange</a></strong></h3>      </div>
      <div id="{{distro}}-question-list" class="panel-body" style="display: none;"></div>
      <div id="{{distro}}-no-question-list" class="panel-body" style="display: none;">
        <p>No questions yet, you can ask one <a href="https://robotics.stackexchange.com/questions/tagged/{{package.name}}+{{distro}}">here</a>.</p>
      </div>
      <div id="{{distro}}-get-question-fail" class="panel-body alert alert-warning" style="display: none;">
        <p>Failed to get question list, you can ticket an issue <a href="https://github.com/ros-infrastructure/rosindex/issues/new" target="_blank" class="alert-link">here</a> </p>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript" src="{{ '/js/package_body_tabs.js' | prepend: site.baseurl }}"></script>

<script src={{ "/js/contribution_suggestions.js" | prepend: site.baseurl }}></script>
<script type="text/javascript">
jQuery(function() {
    // Update the count on the links to the contribution suggestions
    updateContributionSuggestionsCountOnPackage('{{package.repo.uri}}');
});
</script>
